﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
using System.IO;
using System.Net.NetworkInformation;
using DalMgr;
namespace WordAddIn2010
{
    public static class Funcs
    {
        /// <summary>
        /// 获取当前版本号
        /// </summary>
        /// <returns></returns>
        public static Version GetSftVersion()
        {
            Assembly AppAssembly = Assembly.GetExecutingAssembly();
            return AppAssembly.GetName().Version;
        }
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        public static void InitPubVal()
        {
            gValue.SftCfgBaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + gValue.SoftName + "\\";
            CreateDir(gValue.SftCfgBaseDir);
            gValue.Dic_Cate = new Dictionary<string, int>();
            gValue.Dic_Cate.Add("Math", 0);
            gValue.Dic_Cate.Add("Physics", 1);
            gValue.Dic_Cate.Add("Chemistry", 2);
            SftDataMgr.SetCfgPath(gValue.SftCfgBaseDir + gValue.SFT_CFG_FILE_NAME);
        }
        /// <summary>
        /// 判断字符串是否为全数字
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static bool IsNum(string _str)
        {
            if (_str.Length == 0)
            {
                return false;
            }
            return Regex.IsMatch(_str, @"^\d+$");
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="dir"></param>
        public static void CreateDir(string dir)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }
        /// <summary>
        /// 判断网络是否连接成功
        /// </summary>
        /// <returns></returns>
        public static bool CheckNetConnection()
        {
            return NetworkInterface.GetIsNetworkAvailable();
        }
        public static bool IsANewDay()
        {
            //获取保存的日期，和当天日期比较，如果是今天日期，则表示已经检测过升级了，当天不再检测升级
            string strDate = SftDataMgr.ReadStr(gValue.SFT_CFG, gValue.SFT_CFG_TODAY);
            if (0 == string.Compare(strDate, DateTime.Today.ToString(gValue.STR_DATE_TIME_FORMAT)))
            {
                return false;
            }
            //如果不是当天日期，表示当天没有进行检测升级，需要检测升级，并写入当前日期
            SftDataMgr.WriteCfg(gValue.SFT_CFG, gValue.SFT_CFG_TODAY, DateTime.Today.ToString(gValue.STR_DATE_TIME_FORMAT));

            return true;
        }
    }
}
